package com.childenglish.utils;

import java.util.ArrayList;
import java.util.List;

/**
 * 数据迁移工具
 * 用于加密现有数据库中的明文邮箱和手机号
 */
public class DataMigrationTool {
    
    /**
     * 生成加密后的用户数据SQL更新语句
     * 用于迁移现有明文邮箱和手机号到加密格式
     */
    public static void main(String[] args) {
        System.out.println("-- ============================================");
        System.out.println("-- 数据迁移脚本：加密现有用户的邮箱和手机号");
        System.out.println("-- 执行此脚本前，请确保已备份数据库");
        System.out.println("-- ============================================\n");
        
        // 定义需要加密的用户数据（用户名 -> {邮箱, 手机号}）
        List<UserData> usersToEncrypt = new ArrayList<>();
        usersToEncrypt.add(new UserData("admin", "admin@childenglish.com", "13800138000"));
        usersToEncrypt.add(new UserData("admin_wang", "admin_wang@childenglish.com", "13800138010"));
        usersToEncrypt.add(new UserData("admin_zhang", "admin_zhang@childenglish.com", "13800138011"));
        usersToEncrypt.add(new UserData("admin_xie", "3014276808@qq.com", "17781530389"));
        usersToEncrypt.add(new UserData("teacher_li", "teacher_li@childenglish.com", "13800138002"));
        usersToEncrypt.add(new UserData("teacher_wang", "teacher_wang@childenglish.com", "13800138020"));
        usersToEncrypt.add(new UserData("teacher_zhang", "teacher_zhang@childenglish.com", "13800138021"));
        usersToEncrypt.add(new UserData("teacher_chen", "teacher_chen@childenglish.com", "13800138022"));
        usersToEncrypt.add(new UserData("parent_zhang", "zhang@test.com", "13800138001"));
        
        System.out.println("-- 更新用户邮箱和手机号为加密格式");
        System.out.println("-- 注意：此脚本会更新现有用户的邮箱和手机号\n");
        
        for (UserData user : usersToEncrypt) {
            String encryptedEmail = user.email != null && !user.email.isEmpty() 
                ? DataEncryptionUtils.encryptSensitiveField(user.email) 
                : null;
            String encryptedPhone = user.phone != null && !user.phone.isEmpty() 
                ? DataEncryptionUtils.encryptSensitiveField(user.phone) 
                : null;
            
            System.out.println("-- 用户: " + user.username);
            if (encryptedEmail != null) {
                System.out.println("UPDATE `sys_user` SET `email` = '" + encryptedEmail + "' WHERE `user_name` = '" + user.username + "';");
            }
            if (encryptedPhone != null) {
                System.out.println("UPDATE `sys_user` SET `phonenumber` = '" + encryptedPhone + "' WHERE `user_name` = '" + user.username + "';");
            }
            System.out.println();
        }
        
        System.out.println("-- ============================================");
        System.out.println("-- 迁移脚本生成完成");
        System.out.println("-- 请将上述SQL语句复制到数据库管理工具中执行");
        System.out.println("-- ============================================");
    }
    
    /**
     * 用户数据内部类
     */
    private static class UserData {
        String username;
        String email;
        String phone;
        
        UserData(String username, String email, String phone) {
            this.username = username;
            this.email = email;
            this.phone = phone;
        }
    }
}

